package test

import config.MyFile
import org.apache.commons.io.FileUtils
import scala.jdk.CollectionConverters._

object GetAnno extends MyFile {

  def main(args: Array[String]): Unit = {
    PantherId
    GosDes
    GOS
    interpro
    pfam
  }

  def PantherId = {
    val path = "D:\\上海农科院大麦基因组/"

    val row =  s"$path/Hordeum_vulgare_TRITEX_panther.txt".readLines.distinct.map(_.split("\t")).filter(_.length>1).tail.groupBy(_.head).map{x=>
      x._1 + "\t" + x._2.map(y=> y(1)).mkString(";")
    }

    FileUtils.writeLines(s"$path/Hordeum_vulgare_TRITEX_panther2.txt".toFile,row.toBuffer.asJava)
  }

  def GosDes = {
    val path = "D:\\上海农科院大麦基因组/"

    val row =  s"$path/Hordeum_vulgare_TRITEX_GOS.txt".readLines.distinct.map(_.split("\t")).filter(_.length>1).tail.map{x=>
      x.tail.mkString("\t")
    }.distinct

    FileUtils.writeLines(s"$path/Hordeum_vulgare_TRITEX_GOSDES.txt".toFile,row.toBuffer.asJava)
  }

  def GOS = {
    val path = "D:\\上海农科院大麦基因组/"

    val row =  s"$path/Hordeum_vulgare_TRITEX_GOS.txt".readLines.distinct.map(_.split("\t")).filter(_.length>1).tail.groupBy(_.head).map{x=>
      x._1 + "\t" + x._2.map(y=> y(1)).mkString(";")
    }

    FileUtils.writeLines(s"$path/Hordeum_vulgare_TRITEX_GOS2.txt".toFile,row.toBuffer.asJava)
  }

  def interpro = {
    val path = "D:\\上海农科院大麦基因组/"
    val row =  s"$path/Hordeum_vulgare_TRITEX_interpro.txt".readLines.distinct.map(_.split("\t")).filter(_.length>1).tail.groupBy(_.head).map{x=>
      x._1 + "\t" + x._2.map(y=> y(1) + ":" + y(2)).mkString(";")
    }
    FileUtils.writeLines(s"$path/Hordeum_vulgare_TRITEX_interpro2.txt".toFile,row.toBuffer.asJava)
  }

  def pfam = {
    val path = "D:\\上海农科院大麦基因组/"
    val row =  s"$path/Hordeum_vulgare_TRITEX_pfam.txt".readLines.distinct.map(_.split("\t")).filter(_.length>1).tail.groupBy(_.head).map{x=>
      x._1 + "\t" + x._2.map(_.last).mkString(";")
    }
    FileUtils.writeLines(s"$path/Hordeum_vulgare_TRITEX_pfam2.txt".toFile,row.toBuffer.asJava)
  }

  def getGff = {
    val path = "D:\\上海农科院大麦基因组\\annotation"
    val l = "D:\\上海农科院大麦基因组/Hordeum_vulgare.IBSC_v2.51.gff3".readLines.filter(!_.startsWith("#")).map(_.split("\t")).filter(_ (2) == "gene")
    val pfamMap = s"$path/Pfam.txt".readLines.map(_.split("\t")).map(x=> x.head -> x.last).toMap
    val goMap = s"$path/GOS.txt".readLines.map(_.split("\t")).map(x=> x.head -> x.last).toMap
    val interproMap =  s"$path/Interpro.txt".readLines.map(_.split("\t")).map(x=> x.head -> x.last).toMap
    val patherMap = s"$path/PantherID.txt".readLines.map(_.split("\t")).map(x=> x.head -> x.last).toMap

    val row =  l.map { x =>
      val gene = x.last.split(";").head.drop(8)
      val chr = x.head
      val start = x(3).toInt
      val end = x(4).toInt
      val stand = x(6)
      val pfam = pfamMap.getOrElse(gene,"")
      val go = goMap.getOrElse(gene,"")
      val interpro = interproMap.getOrElse(gene,"")
      val pather = patherMap.getOrElse(gene,"")
      (gene,chr,start,end,stand,pfam,go,interpro,pather)
    }
    println(row.length)
  }

}
